package xyz.yq56.glue_tool.view;

import java.util.Map;

import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListSelectionModel;

import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;

import com.intellij.openapi.diagnostic.Logger;
import com.intellij.ui.CollectionListModel;
import com.intellij.ui.SimpleListCellRenderer;
import com.intellij.ui.ToolbarDecorator;
import com.intellij.ui.components.JBList;

import xyz.yq56.glue_tool.config.GlueToolConfig;
import xyz.yq56.glue_tool.dialog.AddUrlDialog;

/**
 * @author yi qiang
 * @date 2023-12-08 15:47:16
 */
public class GlueToolWindow {
    private static final Logger LOGGER = Logger.getInstance(GlueToolWindow.class);
    // 这里不搞成static,新增和删除就没法立即刷新
    private static JBList<String> urlList = new JBList<>();
    private JPanel mainPanel;
    private JPanel urlPanel;
    private GlueToolConfig.Config config;

    public GlueToolWindow() {
        refreshConfig();
    }

    public JPanel getMainPanel() {
        return mainPanel;
    }

    private void createUIComponents() {

        config = GlueToolConfig.getInstance().getState();

        assert config != null;

        LOGGER.debug("---------------当前配置-----------");
        config.items.forEach((k, v) -> LOGGER.debug(k + " : " + v));

        urlList = initUrlList();

        urlPanel = ToolbarDecorator
                .createDecorator(urlList)
                .setAddAction(button -> {
                    AddUrlDialog dialog = new AddUrlDialog();
                    if (dialog.showAndGet()) {
                        String url = dialog.getUrl();
                        if (StringUtils.isNotEmpty(url)) {
                            Map<String, String> items = config.items;
                            System.out.println("添加: " + url);
                            items.put(url, null);
                            refreshConfig();
                        }
                    }
                }).setRemoveAction(anActionButton -> {
                    Map<String, String> items = config.items;
                    System.out.println(("删除: " + urlList.getSelectedValue()));
                    items.remove(urlList.getSelectedValue());
                    refreshConfig();
                }).createPanel();

    }

    private JBList<String> initUrlList() {
        urlList = new JBList<>(new CollectionListModel<>(Lists.newArrayList()));
        urlList.setCellRenderer(new SimpleListCellRenderer<>() {
            @Override
            public void customize(@NotNull JList<? extends String> list, String value, int index, boolean selected,
                                  boolean hasFocus) {
                setText(value);
            }
        });
        urlList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        urlList.setSelectedIndex(0);
        refreshConfig();
        return urlList;
    }

    private void refreshConfig() {
        if (null != config && config.items != null) {
            System.out.println("刷新: " + config.items.keySet().size());
            urlList.setModel(new CollectionListModel<>(config.items.keySet()));
        }
    }
}
